<template>
  <div
    class="_message_container_"
    :class="{
      show: isShow,
    }"
  >
    <span>{{ message }}</span>
  </div>
</template>

<style lang="scss" scoped>
$height: 40px;

._message_container_ {
  position: fixed;
  top: -40px;
  left: 50%;
  opacity: 0;
  transform: translate(-50%, 0);
  background-color: #ffffff;
  color: #666666;
  border-radius: 10px;
  height: $height;
  line-height: $height;
  font-size: 14px;
  padding: 0 20px;
  box-shadow: 0 3px 20px rgba(136, 136, 136, 0.3);
  z-index: 50;

  &.show {
    transition: all 200ms;
    top: 40px;
    opacity: 1;
  }
}
</style>

<script lang="ts">
import { ref, defineExpose } from "vue";

export default {
  name: "Message",
};

export type MessageExpose = {
  show: (msg: string) => void;
};
</script>

<script setup lang="ts">
// 消息
const message = ref(" ");
// 是否显示
const isShow = ref(false);
// 显示时间
const duration = 3000;
// 计时器
let timer: any = null;

function show(msg: string) {
  message.value = msg;

  if (isShow.value) {
    isShow.value = false;
    setTimeout(() => {
      isShow.value = true;
    }, 100);
  } else {
    isShow.value = true;
  }

  clearTimeout(timer);
  timer = setTimeout(() => {
    isShow.value = false;
  }, duration);
}

defineExpose<MessageExpose>({
  show,
});
</script>
